﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTVK05APP : DocumentBase
    {
        #region=============Properties=======================
        string Mandant = "00";
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        JLTVK05APPData m_Data;
        INTEXDBC INTEXDBC = null;
        public DataSet setResult;
        JLTVK05APPReport m_rpt;
        string UserID = "";
        public bool IsUpdateAll
        {
            get
            {
                if (UpdateAll.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    UpdateAll.IsChecked = true;
                }
                else
                {
                    UpdateAll.IsChecked = false;
                }
            }
        }
        public bool IsUpdateAll1
        {
            get
            {
                if (UpdateAll1.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    UpdateAll1.IsChecked = true;
                }
                else
                {
                    UpdateAll1.IsChecked = false;
                }
            }
        }
        public bool IsUpdateAll2
        {
            get
            {
                if (UpdateAll2.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    UpdateAll2.IsChecked = true;
                }
                else
                {
                    UpdateAll2.IsChecked = false;
                }
            }
        }
        public bool IsUpdateAll3
        {
            get
            {
                if (UpdateAll3.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    UpdateAll3.IsChecked = true;
                }
                else
                {
                    UpdateAll3.IsChecked = false;
                }
            }
        }
        public bool IsUpdateAll4
        {
            get
            {
                if (UpdateAll4.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    UpdateAll4.IsChecked = true;
                }
                else
                {
                    UpdateAll4.IsChecked = false;
                }
            }
        }
        #region==========CollectionViewSource=================
        SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetData");
            }
        }
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if (vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }



        string art
        {
            get
            {
                if (dwVKPOS == null)
                    return "";
                return dwVKPOS["VKPART"].ToString();

            }
        }
        #endregion
        int total = 0;
        DataSet setCADMap;
        public JLTVK05APP()
        {
            InitializeComponent();
            this.Name = "生产审核";
            ZoneAccount.Visibility = Visibility.Hidden;
            ZoneAuditing.Visibility = Visibility.Hidden;
            ZoneOrder.Visibility = Visibility.Hidden;
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!IsSetUp)
            {
                ApplicationBase.Quit();
            }
            base.OnClosing(e); 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
                    #region============TESTINGDATA================
                    txtUserID.Text = ApplicationBase.User;
                    txtPassword.Password = ApplicationBase.Pwd;
                    #endregion
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                Mandant = (string)dicParamater["MANDANT"];
                INTEXDBC = new INTEX.DataBase.Tools.INTEXDBC(OracleDB, proxy);
                m_Data = new JLTVK05APPData(OracleDB, proxy);
                #region============TESTINGDATA================
                txtVKPAUFKRS.Text = "C0";
                txtVKPAUFLFD.Text = "100227";
                txtVKPAUFKRS.Text = "J3";
                txtVKPAUFLFD.Text = "002001";
                txtUserID.Text = (string)dicParamater["User"];
                txtPassword.Password = (string)dicParamater["Pwd"];
                vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged;
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (dwVKPOS == null)
                return;
            if (dwVKPOS["VKPLIEFTERM"].ToString().Trim() == "0")
            {
                dwVKPOS["VKPLIEFTERM"] = dwVKPOS["VKPWUTERM"];
            }
        }




        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            ZoneAccount.Visibility = Visibility.Hidden;
            ZoneAuditing.Visibility = Visibility.Hidden;
            ZoneOrder.Visibility = Visibility.Hidden;
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count == 0)
                return;
            if (setResult.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            DataRow dwUser = setResult.Tables[0].Rows[0];
            txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
            UserID = userID;
            m_Data.UserID = userID;
            this.UserID = userID;

            m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
            m_Data.MANDANT = Mandant;
            m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();

            btnGetVKPOS.IsEnabled = true;

            IsBelongs2V9.IsChecked = false;

            ZoneOrder.Visibility = Visibility.Visible;
            ZoneAccount.Visibility = Visibility.Visible;
            if (MemberV9.Text.Contains(userID.Trim() + ";"))
            {

                IsBelongs2V9.IsChecked = true;

                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "开始审单";
                ZoneAccount.Visibility = Visibility.Hidden;
                ZoneAuditing.Visibility = Visibility.Visible;
            }
            if (Administration.Text.Contains(userID.Trim() + ";"))
            {
                ZoneAccount.Visibility = Visibility.Hidden;
                ZoneAuditing.Visibility = Visibility.Hidden;
                ZoneOrder.Visibility = Visibility.Hidden;
            }

            EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
        }


        #endregion
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void btnDistribute_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType, true);
        }
        public void GenerateReport(string PrintType, bool isapproved = false)
        {
            try
            {
                //VKPFREITERM	VKPVERSTERM	VKPLIEFTERM	VKPLIEFEING
                using (new LongOperation(this, "读取符合条件的记录"))
                {

                    JLTVK05APPData m_DX = new JLTVK05APPData(dicParamater);
                    JLTVK05APPReport m_rpt = new JLTVK05APPReport(dicParamater);
                    m_rpt.dsSource = SetData;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DX.exAgent;
                    m_rpt.OracleDB = this.OracleDB;
                    m_rpt.proxy = this.proxy;
                    m_rpt.IsApproved = isapproved;
                    m_rpt.UserID = this.txtUserID.Text;
                    m_rpt.WriteExcelReport(PrintType);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (DataRow row in SetData.VKPOS.Rows)
                {
                    INTEXDBC.UpdateVKPOS(row, "", 3);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnGetVKPOS_Click(object sender, RoutedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            try
            {
                SetData.Clear();
                DataSet setResult = new DataSet();
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataRow dwVKKOPF = SetData.VKKOPF.NewVKKOPFRow();
                    dwVKKOPF["VKKMANDANT"] = "00";
                    dwVKKOPF["VKKAUFKRS"] = m_Data.Left(txtVKPAUFKRS.Text + "  ", 2);
                    dwVKKOPF["VKKAUFLFD"] = m_Data.Right("0000000" + txtVKPAUFLFD.Text, 6);
                    setResult = m_Data.GetData(dwVKKOPF);
                    SetData.Merge(setResult);
                    if (vwVKPOS.View != null)
                    {
                        vwVKPOS.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        }

        private void txtVKPAUFLFD_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnGetVKPOS_Click(null, null);
            }
        }

        private void Txtdate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e != null)
            {
                if (e.Key != Key.Enter)
                    return;
            }

            TextBox tb = (TextBox)sender;
            if (tb == null)
                return;
            string name = tb.Name;
            switch (name)
            {
                case "txtDate":
                    if (txtDate.Text.Trim().Length < 8)
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (!txtDate.Text.StartsWith("20"))
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (IsUpdateAll)
                    {
                        foreach (DataRow row in SetData.VKPOS.Rows)
                        {
                            row["VKPLIEFTERM"] = txtDate.Text;
                            row["VKPTERMSTA"] = "80";                            
                        }
                    }
                    break;

                case "txtDate1":
                    if (txtDate1.Text.Trim().Length < 8)
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (!txtDate1.Text.StartsWith("20"))
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (IsUpdateAll1)
                    {
                        foreach (DataRow row in SetData.VKPOS.Rows)
                        {
                            row["VKPVERZTERM1"] = txtDate1.Text;
                        }
                    }
                    break;
                case "txtDate2":
                    if (txtDate2.Text.Trim().Length < 8)
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (!txtDate2.Text.StartsWith("20"))
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (IsUpdateAll2)
                    {
                        foreach (DataRow row in SetData.VKPOS.Rows)
                        {
                            row["VKPVERZTERM2"] = txtDate.Text;
                        }
                    }
                    break;
                case "txtDate3":
                    if (txtDate3.Text.Trim().Length < 8)
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (!txtDate3.Text.StartsWith("20"))
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (IsUpdateAll3)
                    {
                        foreach (DataRow row in SetData.VKPOS.Rows)
                        {
                            row["VKPVERZTERM3"] = txtDate3.Text;
                        }
                    }
                    break;
                case "txtDate4":
                    if (txtDate4.Text.Trim().Length < 8)
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (!txtDate4.Text.StartsWith("20"))
                    {
                        MessageBox.Show("格式不是20131231");
                        return;
                    }
                    if (IsUpdateAll4)
                    {
                        foreach (DataRow row in SetData.VKPOS.Rows)
                        {
                            row["VKPVERZTERM4"] = txtDate4.Text;
                        }
                    }
                    break;
            }
        }

        private void dPicker4_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;
            if (date == null)
            {
                txtDate4.Text = " ";
            }
            else
            {
                txtDate4.Text = date.Value.Year + Helper.Right("00" + date.Value.Month, 2) + Helper.Right("00" + date.Value.Day, 2);

            }
            Txtdate_KeyDown(txtDate4, null);
        }
        private void dPicker3_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;
            if (date == null)
            {
                txtDate3.Text = " ";
            }
            else
            {
                txtDate3.Text = date.Value.Year + Helper.Right("00" + date.Value.Month, 2) + Helper.Right("00" + date.Value.Day, 2);

            }
            Txtdate_KeyDown(txtDate3, null);
        }
        private void dPicker2_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;
            if (date == null)
            {
                txtDate2.Text = " ";
            }
            else
            {
                txtDate2.Text = date.Value.Year + Helper.Right("00" + date.Value.Month, 2) + Helper.Right("00" + date.Value.Day, 2);
            }
            Txtdate_KeyDown(txtDate2, null);
        }
        private void dPicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;
            if (date == null)
            {
                txtDate1.Text = " ";
            }
            else
            {
                txtDate1.Text = date.Value.Year + Helper.Right("00" + date.Value.Month, 2) + Helper.Right("00" + date.Value.Day, 2);
            }
            Txtdate_KeyDown(txtDate1, null);
        }
        private void dPicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;
            if (date == null)
            {
                txtDate.Text = " ";
            }
            else
            {
                txtDate.Text = date.Value.Year + Helper.Right("00" + date.Value.Month, 2) + Helper.Right("00" + date.Value.Day, 2);
            }
            Txtdate_KeyDown(txtDate, null);
        }
    }
}
